package com.my.mymafengwo.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.smallstrong.mafengwopro.R;
import com.my.mymafengwo.base.BaseActivity;
import com.my.mymafengwo.fragment.AcountLoginFragment;
import com.my.mymafengwo.fragment.MesLoginFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by smallstrong on 2015/12/6.
 * step
 */
public class MineLoginActivity extends BaseActivity {
    private ViewPager viewPager;
    private List<Fragment> list;
    private RadioGroup rgLogin;
    private ImageView ivLineLeft, ivLineRight,ivBack;
    private RadioButton rbAcount, rbMes;

    @Override
    protected void loadXml() {
        setContentView(R.layout.activity_mine_login);
    }

    @Override
    protected void getIntentData(Bundle savedInstanceState) {

    }

    @Override
    protected void initViews() {
        viewPager = (ViewPager) findViewById(R.id.vp);
        rgLogin = (RadioGroup) findViewById(R.id.rg_login);
        ivLineLeft = (ImageView) findViewById(R.id.iv_line_left);
        ivLineRight = (ImageView) findViewById(R.id.iv_line_right);
        rbAcount = (RadioButton) findViewById(R.id.rb_acount);
        rbMes = (RadioButton) findViewById(R.id.rb_mes);
        ivBack = (ImageView) findViewById(R.id.iv_icon_back_orange);
    }

    @Override
    protected void registerListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    rbAcount.setChecked(true);
                    ivLineLeft.setVisibility(View.VISIBLE);
                    ivLineRight.setVisibility(View.INVISIBLE);
                } else {
                    rbMes.setChecked(true);
                    ivLineLeft.setVisibility(View.INVISIBLE);
                    ivLineRight.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        rgLogin.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_acount:
                        viewPager.setCurrentItem(0);
                        ivLineLeft.setVisibility(View.VISIBLE);
                        ivLineRight.setVisibility(View.INVISIBLE);
                        break;
                    case R.id.rb_mes:
                        viewPager.setCurrentItem(1);
                        ivLineLeft.setVisibility(View.INVISIBLE);
                        ivLineRight.setVisibility(View.VISIBLE);
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {
        list = new ArrayList<Fragment>();
        list.add(new AcountLoginFragment());
        list.add(new MesLoginFragment());
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    }

    class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}
